﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Way.Bus.Wms.Controllers.BusinessLogic;
using Way.Contract.MVC;
using Way.Bus.WMS.ST.Model;
using Way.EAP.DataAccess.Regulation;
using Way.Contract.Data.ControllerData;

namespace Way.Bus.WMS.ST.Controllers.OutOrder
{
    /// <summary>
    /// 借货单
    /// </summary>
    public class ST_ConductioController : ConductioController
    {    //构造方法 controller与model绑定
         public ST_ConductioController() : base(new ST_Conductio()){ }
         public ST_ConductioController(IModel model) : base(model) { }


        protected override void OnInitViewChildItem(Way.Contract.Data.ModelData data, Way.Contract.Data.ItemData item)
        {
            base.OnInitViewChildItem(data, item);
            if (data.name.EndsWith("StockOutNoticeMaterials"))
                data.visible = false;
            if (data.name.EndsWith("ST_Conductio"))
            {
                data.title = "借货单";
                if (item.IsField("sourcecode"))
                    item.visible = false;
                if (item.IsField("code"))
                {
                    item.title = "借货单号";
                    item.disabled = true;
                }
                if (item.IsField("towhno"))
                    item.title = "发货仓库";
                if (item.IsField("CLIENTNO"))
                    item.title = "发货单位";
                if (item.IsField("WORKERID"))
                    item.title = "借货负责人";
                if (item.IsField("STAY3"))
                {
                    item.visible = true;
                    item.title = "借货人";
                }
                if (item.IsField("SOURCENAME") || item.IsField("SOURCEOUTCODE") ||
                    item.IsField("INOUTTIME") || item.IsField("LOGCODE") || item.IsField("STOCKTYPE")
                    || item.IsField("STATE"))
                    item.visible = false;
                if (item.IsField("FINISHTIME"))
                    item.title = "预计还货日期";
            }
            if (data.name.EndsWith("StockOutOrderMaterials"))
            {
                ST_OutOrderController.InitOrderMaterials(data, item);
            }
            if (data.name.EndsWith("Sequence"))
            {
                data.visible = true;
                if (item.IsField("SEQUENCECODE"))
                    item.visible = true;
                else
                    item.visible = false;
            }
        }
        protected override void OnForeignLoading(IModel model, Way.Contract.Data.ControllerData.loadItem item)
        {
            List<SQLWhere> list = new List<SQLWhere>();
            //物流公司与客户是同一张表，ftype=1是物流公司 ftype=2是客户
            if (base.fdata.filedname == "CLIENTNO")
            {
                list.AddRange(item.whereList);
                SQLWhere w = new SQLWhere();
                w.ColumnName = "ftype";
                w.Value = "2";
                list.Add(w);
                item.whereList = list.ToArray();
            }

            //当查询借货单位、借货负责人、部门时，不增加角色数据权限
            if (base.fdata.filedname == "CONDUCTIOCOMPANY" || base.fdata.filedname == "WORKERID" || base.fdata.filedname == "BUMEN")
            {
                item.where = "NotAddRoleData";

            }
            //if (this.fdata.filedname == "RECEIPTCONTACTPERSON")
            //{
            //    if (this.ActionItem["CLIENTNO"] != null && !string.IsNullOrEmpty(this.ActionItem["CLIENTNO"].ToString()))
            //    {
            //        list.AddRange(item.whereList);
            //        SQLWhere w = new SQLWhere();
            //        w.ColumnName = "PID";
            //        w.Value = this.ActionItem["CLIENTNO"].ToString();
            //        list.Add(w);
            //        item.whereList = list.ToArray();
            //    }
            //    else
            //        throw new Exception("请先选择客户");
            //}
        }

        protected override ReadTable OnForeignLoad(IModel model, loadItem item)
        {
            if (this.fdata.foreignobject.EndsWith("Materials"))
            {
                if (this.fdata.filedname == "MATERIALCODE")
                {
                    //if (this.ActionItem["TOWHNO"] != null && !string.IsNullOrEmpty(this.ActionItem["TOWHNO"].ToString()))
                    //{
                    //    item.rowsql = Common.GetNoticeMaterialsSQL(item.rowsql, this.ActionItem["TOWHNO"].ToString());
                    //}
                    //else
                    //    throw new Exception("请先选择仓库");
                
                    if (this.ActionItem["CLIENTNO"] != null && !string.IsNullOrEmpty(this.ActionItem["CLIENTNO"].ToString()))
                    {
                        item.rowsql = Common.GetNoticeMaterialsSQL(item.rowsql, this.ActionItem["CLIENTNO"].ToString());
                    }
                    else
                        throw new Exception("请先选择客户");
                    
                }
            }
            return base.OnForeignLoad(model, item);
        }
    
    }
}
